import { defineStore } from "pinia";
import { ref } from "vue";
import { GetHostGroupList, GetHostGroupTree } from "~/api/cmdb";
const useHostGroupTree = defineStore("hostGroupTree", () => {
  const hostGroupTreeData = ref([]);
  const hostGroupRef = ref(null);
  const deptDefaultProps = ref({
    children: "children",
    label: "groupName",
  });
  // 主机组树形数据
  const HostGroupList = ref([]);
  // 主机组ID 用于删选主机
  const groupID = ref(1);

  const initHostGroup = async () => {
    const res = await GetHostGroupTree();
    if (res) {
      hostGroupTreeData.value = res;
    }
  };

  // 初始化主机组方法
  const initHostGroupList = async () => {
    const res = await GetHostGroupList();
    if (res) {
      HostGroupList.value = res;
    }
  };

  // 主机组相关 筛选节点
  const filterNode = (value, data) => {
    if (!value) return true;
    console.log("data = ", data);
    return data.groupName.indexOf(value) !== -1;
  };

  // 点击触发事件
  const handleNodeClick = (row) => {
    groupID.value = row.id;
    //  TODO 遗留问题 优化
    // initPage();
  };

  return {
    hostGroupTreeData,
    hostGroupRef,
    deptDefaultProps,
    initHostGroup,
    initHostGroupList,
    filterNode,
    handleNodeClick,
  };
});

export default useHostGroupTree;
